﻿using Core.Info;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Log
{
    /// <summary>
    /// 应用日志组件工厂
    /// </summary>
    public static class AppLogFacotry
    {
        /// <summary>
        /// 默认日志配置文件名
        /// </summary>
        private static readonly string defaultLogConfigFileName = "log.settings.json";

        /// <summary>
        /// 日志配置
        /// </summary>
        public static IConfigurationRoot LogConfiguration { get; private set; }

        static AppLogFacotry()
        {
            //读取配置文件
            var configuration = new ConfigurationBuilder()
                .SetBasePath(AppInfo.ConfigDirectory)
                .AddJsonFile(defaultLogConfigFileName, false, true);
            string appSettingsConfigPath = Path.Combine(AppInfo.RootDirectory, "appsettings.json");
            if (File.Exists(appSettingsConfigPath))
            {
                configuration = new ConfigurationBuilder()
                    .SetBasePath(AppInfo.RootDirectory)
                    .AddJsonFile("appsettings.json", false, true)
                    .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true);
            }
            LogConfiguration = configuration.Build();
        }

        /// <summary>
        /// 创建日志组件
        /// </summary>
        /// <returns></returns>
        public static ILogger CreateLogger(string categoryName = "")
        {
            string loggerType = (LogConfiguration?["LoggerType"] ?? "Microsoft").ToLower();
            if(loggerType == "Serilog".ToLower() || loggerType == "1")
            {
                return SerilogLogger.CreateLogger(categoryName);
            }
            else
            {
                return MicrosoftLogger.CreateLogger(categoryName);
            }
        }

        /// <summary>
        /// 创建日志组件
        /// </summary>
        /// <returns></returns>
        public static ILogger CreateLogger<T>()
        {
            string loggerType = (LogConfiguration?["LoggerType"] ?? "Microsoft").ToLower();
            if (loggerType == "Serilog".ToLower() || loggerType == "1")
            {
                return SerilogLogger.CreateLogger<T>();
            }
            else
            {
                return MicrosoftLogger.CreateLogger<T>();
            }
        }

        /// <summary>
        /// 创建日志组件
        /// </summary>
        /// <returns></returns>
        public static ILogger CreateLogger(Type type)
        {
            string loggerType = (LogConfiguration?["LoggerType"] ?? "Microsoft").ToLower();
            if (loggerType == "Serilog".ToLower() || loggerType == "1")
            {
                return SerilogLogger.CreateLogger(type);
            }
            else
            {
                return MicrosoftLogger.CreateLogger(type);
            }
        }
    }
}
